#include "opensourcehardwarehub.h"

#include <httpclient.h>

OpenSourceHardWareHub::OpenSourceHardWareHub(Type type) : m_type(type) {}

QString OpenSourceHardWareHub::name()
{
    switch (m_type) {
    case Hub:
        return "开源硬件平台";
    case Article:
        return "开源硬件平台 - 文章/工程";
        break;
    }

}

QString OpenSourceHardWareHub::prefix()
{
    return "https://oshwhub.com/search?wd=";
}

Wiki::SearchMode OpenSourceHardWareHub::searchMode() {

    switch (m_type) {
    case Hub:
        return Text;
    case Article:
        return ListItem;
        break;
    }
}

QList<QListWidgetItem *> OpenSourceHardWareHub::searching_item(const QString &text) {
    QList<QListWidgetItem*> list;

    QVariantList lists;
    bool article_to_project = false;
    if (text.isEmpty()) {
        Request req("https://oshwhub.com/api/articles");
        req.addQuery("page", "1");
        req.addQuery("pageSize","100");
        req.addQuery("tab","all");
        req.addQuery("filter","latest");
        lists = HttpClient::instance().get(req).toMap().value("result").toMap().value("lists").toList();
    } else {
        QVariantMap data;
        data.insert("type","article");
        data.insert("keywords", text);
        data.insert("category","all");
        data.insert("sort","");
        data.insert("page",1);
        data.insert("pageSize",100);
        Request req("https://oshwhub.com/api/search/data");
        req.setData(data);
        lists = HttpClient::instance().post(req).toMap().value("result").toMap().value("lists").toList();

        if (lists.empty()) {
            article_to_project = true;
            data.insert("type","project");
            req.setData(data);
            lists = HttpClient::instance().post(req).toMap().value("result").toMap().value("lists").toList();
        }
    }

    foreach (auto var, lists) {
        auto title = article_to_project
                        ? "[工程]" + var.toMap().value("name").toString()
                        : "[文章]" + var.toMap().value("title").toString();

        auto path = article_to_project
                        ? "https://oshwhub.com/" + var.toMap().value("path").toString()
                        : "https://oshwhub.com/article/" + var.toMap().value("path").toString();

        auto created_at = var.toMap().value("created_at").toString();
        QDateTime datetime = QDateTime::fromString(created_at, "yyyy-MM-ddTHH:mm:ss.zzzZ");
        // 2022-12-04T14:11:05.000Z

        auto v = newItem();
        v->setText(datetime.toString("yyyy-MM-dd") +": " + title);
        v->setData(990, path);
        list << v;
    }

    return list;
}


bool OpenSourceHardWareHub::forward_to_content()
{
    switch (m_type) {
    case Hub:
        return true;
    case Article:
        return false;
        break;
    }
}

QString OpenSourceHardWareHub::content(const QString &text)
{
    if (text.isEmpty()) {
        return "https://oshwhub.com/explore";
    }
    return prefix() + text;
}

QString OpenSourceHardWareHub::content_item(const QListWidgetItem *item) {
    return item->data(990).toString();
}
